Lobby = States:Register("lobby", Class:Create())
Lobby.vehicles = {}

function onSpawnCar(player, _, id)
	id = getVehicleModelFromName(id)
	
	if tonumber(id) then
		if Lobby.vehicles[player] then
			destroyElement(Lobby.vehicles[player])
		end
		
		local x,y,z = getElementPosition(player)
		local _,_,r = getElementRotation(player)
		
		Lobby.vehicles[player] = createVehicle(id, x, y, z, 0, 0, r)
		warpPedIntoVehicle(player, Lobby.vehicles[player])
		
		outputChatBox("* Car spawned", player, 0, 255, 0)
		return false;
	end
	
	outputChatBox("* Wrong car id/name", player, 255, 0, 0)
	return false;
end

function onToggleTeam(player, _)
	local t1 = getTeamFromName(teams[1][1])
	local t2 = getTeamFromName(teams[2][1])
	
	if getPlayerTeam(player) == t1 then
		setElementData(player, "skin", teams[2][5])
		setPlayerTeam(player, t2)
	else
		setElementData(player, "skin", teams[1][5])
		setPlayerTeam(player, t1)
	end
	
	Player:toObject(player):Spawn()
end

function onAdminArena(player, _, id)
	startResource(getResourceFromName("arena_" .. tostring(id)))
	resource = getResourceFromName("arena_" .. tostring(id))
	outputChatBox("* Arena " .. tostring(id) .. " started by " .. getPlayerName(player), getRootElement(), 255, 255, 0)
end

function Lobby:Start()
	-- For yall nubs
	addCommandHandler("car", onSpawnCar)
	addCommandHandler("toggleteam", onToggleTeam)
	
	-- Admin arenas/bases
	addCommandHandler("arena", onAdminArena)
end

function Lobby:Spawn(Source)
	local l = spawns[math.random(1, #spawns)]
	
	spawnPlayer(Source.element, l[1], l[2], l[3], math.random(0,359), Source:Data("skin"))
end

function Lobby:Wasted(Source)
	Source:Spawn()
end

States:Change("lobby")